package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ShareKey;
import com.shaoshaohuo.app.entity.FragmentNewCarEntity;
import com.shaoshaohuo.app.entity.NewCarlistEntity;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.ui.NewDetailsActivity;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.StringUtil;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.shaoshaohuo.app.view.WhView;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/6/6.
 */

public class NewCarFragmentAdapter extends MyBaseAdapter<NewCarlistEntity> implements WhView.OnWhlistener, CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private final LayoutInflater from;

    private String type;//1,2  1是普通水果过来的2是采摘过来的

    public NewCarFragmentAdapter(Context ctx, List<NewCarlistEntity> list, boolean flag, String type) {
        super(ctx, list, flag);
        from = LayoutInflater.from(ctx);
        this.type = type;
    }

    @Override
    public View getView(int pos, View view, ViewGroup group) {
        ViewHolder myholde;
        if (view == null) {
            view = from.inflate(R.layout.fragment_newcarshoplayout, null);
            myholde = new ViewHolder();
            myholde.isdelete = (CheckBox) view.findViewById(R.id.carshoplit_seleter);
            myholde.whView = (WhView) view.findViewById(R.id.carshoplit_wh);
            myholde.iv_shoppic = (ImageView) view.findViewById(R.id.iv_shoppic);
            myholde.iv_guoqi = (ImageView)   view.findViewById(R.id.iv_guoqi);
            myholde.tv_shopdate = (TextView) view.findViewById(R.id.tv_shopdate);
            myholde.tv_shoppname = (TextView) view.findViewById(R.id.tv_shoppname);
            myholde.tv_shopmoney = (TextView) view.findViewById(R.id.tv_shopmoney);
            myholde.linearLayout = (LinearLayout) view.findViewById(R.id.list_lin_item);
            myholde.car_pick_share = (LinearLayout) view.findViewById(R.id.car_pick_share);
            myholde.tv_mbn_details = (TextView) view.findViewById(R.id.tv_mbn_details);
            view.setTag(myholde);
        } else {
            myholde = (ViewHolder) view.getTag();
        }
        myholde.linearLayout.setTag(pos);
        myholde.linearLayout.setOnClickListener(this);
        NewCarlistEntity newCarlistEntity = list.get(pos);
        String num = newCarlistEntity.getDataBean().getNum();
        boolean ischecked = newCarlistEntity.ischecked();
        int postion = newCarlistEntity.getPostion();
        FragmentNewCarEntity.DataBean dataBean = newCarlistEntity.getDataBean();
        //图片地址
        String img = dataBean.getImg();
        //物品名称
        String names = dataBean.getNames();
        //单价
        String price = dataBean.getPrice();
        //描述
        String details = dataBean.getDetails();
//        if (type.equals(1)) {
        myholde.tv_shopdate.setVisibility(View.VISIBLE);
        myholde.tv_shopdate.setText("(一件起售 " + details + ")");
        double aFloat = Double.valueOf(price);
        int integer = Integer.valueOf(num);
        int mbn_details = dataBean.getMbn_details();
        double v = mbn_details * aFloat * integer;
        myholde.tv_shopmoney.setText("¥" + StringUtil.zeor(v + ""));
        if (dataBean.getMbn_detailsType()==1) {
            myholde.tv_mbn_details.setText("约" +newCarlistEntity.getDataBean().getMbn_details() + "斤/件");
        } else if (dataBean.getMbn_detailsType() ==2) {
            myholde.tv_mbn_details.setText("约" +newCarlistEntity.getDataBean().getMbn_details() + "个/件");
        }
        if(dataBean.getGoodStatus().equals("2")){
            myholde.iv_guoqi.setVisibility(View.VISIBLE);
        }else {
            myholde.iv_guoqi.setVisibility(View.GONE);
        }
//        } else if (type.equals(2)) {
//            myholde.tv_shopdate.setVisibility(View.GONE);
//            myholde.car_pick_address_linearLayout.setVisibility(View.VISIBLE);
//            myholde.car_pick_address.setText("地址");
//            myholde.tv_shopmoney.setText("80/人");
//        }
        myholde.tv_shoppname.setText(names);
        //num*price
        Picasso.with(context).load(img).placeholder(R.mipmap.zhanzhanzhan).into(myholde.iv_shoppic);
        myholde.whView.setNum(Integer.valueOf(num));
        Log.e("TAG", "getView: " + ischecked + "pos" + pos);
        myholde.isdelete.setTag(pos);
        myholde.isdelete.setChecked(ischecked);
        myholde.isdelete.setOnCheckedChangeListener(this);
        myholde.whView.setTag(pos);

            myholde.whView.setWhlistener(this);
//        }
        return super.getView(pos, view, group);

    }

    private String strdobule(Float d) {
        String s = String.valueOf(d);
        Log.e("TAG", "strdobule: " + s);
        if (s.indexOf(".") != -1) {
            Log.e("TAG", "strdobule: aaaaaaaaaaaaaaaaaaaaaaaaaaa" + s.indexOf("."));
            int i = s.indexOf(".");
            if (s.length() - i + 1 < 2) {
                s += "0";
            }

        }
        return s;
    }

    @Override
    public void onwh(final View view, final int num, int type) {
        int tag = (int) view.getTag();
        String goodStatus = list.get(tag).getDataBean().getGoodStatus();
        if (goodStatus.equals("2")){
            ToastUtil.toast("商品已过期");
        }else {
            final int pos = (int) view.getTag();

            String catid = list.get(pos).getDataBean().getCatid();
//        ToastUtil.toast("" + type);
            //type= 0 加  =1 減，
            if (type == 0) {
                //加
                ShopHttpConfig.addshop(context, catid, new Okhttputils.OkutListener() {
                    @Override
                    public void error(Call call, IOException e) {

                    }

                    @Override
                    public void error(Call call, int code) {

                    }

                    @Override
                    public void suseff(Call call, Response response, Object o) {

                        Log.d("TAG", "onCheckedChanged: 成功");
                        view.post(new Runnable() {
                            @Override
                            public void run() {
                                list.get(pos).getDataBean().setNum((num + 1) + "");
                                ((WhView) view).setNum(num + 1);
                                notifyDataSetChanged();
                                if (adapterListener != null) {
                                    adapterListener.whlistener();
                                }
                            }
                        });
                    }
                });
            } else {
                ShopHttpConfig.wheathershop(context, catid, new Okhttputils.OkutListener() {
                    @Override
                    public void error(Call call, IOException e) {

                    }

                    @Override
                    public void error(Call call, int code) {

                    }

                    @Override
                    public void suseff(Call call, Response response, Object o) {
                        Log.d("TAG", "onCheckedChanged: 減成功");
                        view.post(new Runnable() {
                            @Override
                            public void run() {
                                list.get(pos).getDataBean().setNum((num - 1) + "");
                                ((WhView) view).setNum(num - 1);
                                notifyDataSetChanged();
                                if (adapterListener != null) {
                                    adapterListener.whlistener();
                                }
                            }
                        });
                    }
                });
            }
//        progressDialog .show();
        }

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int pos = (int) buttonView.getTag();
        Log.d("TAG", "onCheckedChanged: " + pos + "isChecked" + isChecked);
        list.get(pos).setIschecked(isChecked);
        if (adapterListener != null) {
            adapterListener.chekedelistener(isChecked);
        }
    }


    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag();

//        ToastUtil.toast("tag");
        String catid = list.get(tag).getDataBean().getCatid();
        String goodStatus = list.get(tag).getDataBean().getGoodStatus();
        if(goodStatus.equals("2")){
            ToastUtil.toast("商品已过期");
            return;
        }
        Intent intent = new Intent(context, NewDetailsActivity.class);
        intent.putExtra(ShareKey.CATID, catid);
        context.startActivity(intent);
    }

    class ViewHolder {
        CheckBox isdelete;
        WhView whView;
        ImageView iv_shoppic,iv_guoqi;
        TextView tv_shoppname, tv_shopdate, tv_shopmoney, tv_mbn_details;
        LinearLayout linearLayout;

        LinearLayout car_pick_share;
    }

    private AdaterListener adapterListener;

    public void setAdapterListener(AdaterListener adapterListener) {
        this.adapterListener = adapterListener;
    }

    public interface AdaterListener {
        void whlistener();

        void chekedelistener(boolean ischecked);
    }

}
